package org.mozhu.simple.rpc.server.network;

import java.nio.channels.SelectionKey;

public class RpcChannel {
    private final String id;
    private final int maxReceiveSize;
    // Track connection and mute state of channels to enable outstanding requests on channels to be
    // processed after the channel is disconnected.
    private boolean disconnected;
    private final TransportLayer transportLayer;
    private Send send;

    public RpcChannel(String id, TransportLayer transportLayer, int maxReceiveSize) {
        this.id = id;
        this.transportLayer = transportLayer;
        this.maxReceiveSize = maxReceiveSize;
        this.disconnected = false;
    }

    public void disconnect() {
        this.disconnected = true;
    }

    public void setSend(Send send) {
        if (this.send != null)
            throw new IllegalStateException("Attempt to begin a send operation with prior send operation still in progress.");
        this.send = send;
        this.transportLayer.addInterestOps(SelectionKey.OP_WRITE);
    }
}
